package com.sduept.nwld.dataserver.controller.statistics;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.springframework.beans.factory.annotation.Autowired;

import com.sduept.bigdata.document.core.CommonDocumentProperties;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.manager.statistics.ProtectDefectsManager;
import com.sduept.nwld.dataserver.model.countanalyse.CountProtectDefect;
import com.sduept.utils.DateUtil;

@Named
@ViewScoped
public class ProtectDefectsController extends AbstractController {

	private static final long serialVersionUID = 7542757945627165030L;
	@Autowired
	private CommonDocumentProperties pros;
	@Autowired
	private ProtectDefectsManager manager;

	private List<CountProtectDefect> countProtectDefects = new ArrayList<>();
	private CountProtectDefect selectCountProtectDefect = new CountProtectDefect();
	private CountProtectDefect countProtectDefect = new CountProtectDefect();
	// 省份
	private String province = "0";
	private List<String> provinces = new ArrayList<String>();
	// 缺陷等级
	private String defectLevel = "0";
	private List<String> defectLevels = new ArrayList<String>();
	// 是否消缺
	private boolean solved;
	// 年份
	private int year;
	private List<Integer> years = new ArrayList<Integer>();
	private StreamedContent templateFile;

	@PostConstruct
	private void init() {
		years = manager.getAllYears();
	}

	public void initData() {
		provinces = manager.getAllProvince();
		defectLevels = manager.getAllDefectLevels();
		queryByCondition();
	}

	public void preCreateCountProtectDefect() {
		countProtectDefect = new CountProtectDefect();
	}

	public void editCountProtectDefect() {
		countProtectDefect = selectCountProtectDefect;
	}

	public void saveCountProtectDefect() {
		if (countProtectDefect != null) {
			CountProtectDefect act = manager.create(countProtectDefect);
			if (countProtectDefect.getId() == null) {// 新建
				countProtectDefects.add(act);
			} else {// 修改
				countProtectDefects.set(countProtectDefects.indexOf(countProtectDefect), act);
				selectCountProtectDefect = new CountProtectDefect();
			}
			addSuccessMessage("保存成功!");
			years.clear();
			init();
			provinces.clear();
			initData();
		}
	}

	public void deleteCountProtectDefect() {
		if (selectCountProtectDefect != null && !selectCountProtectDefect.getId().isEmpty()) {
			manager.delete(selectCountProtectDefect.getId());
			countProtectDefects.remove(selectCountProtectDefect);
			addSuccessMessage("删除成功!");
			years.clear();
			init();
			provinces.clear();
			initData();
			selectCountProtectDefect = new CountProtectDefect();
		} else {
			addErrorMessage("请选择要删除的数据!");
		}
	}

	public void queryByCondition() {
		if (StringUtils.isEmpty(defectLevel)) {
			defectLevel = "";
		}
		countProtectDefects = manager.query(year, province, defectLevel, solved);
	}

	public void reset() {
		year = 0;
		province = "0";
		defectLevel = "0";
		solved = true;
		queryByCondition();
	}

	/**
	 * 文件下载
	 */
	public void fileDownloadView() {
		InputStream stream = null;
		try {
			stream = new FileInputStream(new File(pros.getTempletePath() + "countProtectDefect.xlsx"));
//			stream = new FileInputStream(new File(pros.getTempletePath()+"countProtectDefect.xlsx"));
			templateFile = new DefaultStreamedContent(stream, "xlsx", "countProtectDefect.xlsx");
		} catch (FileNotFoundException e) {
			addErrorMessage("下载文件失败，请联系技术人员！");
			e.printStackTrace();
		}
	}

	public void chooseEquipment() {
		Map<String, Object> options = new HashMap<String, Object>();
		options.put("resizable", true);
		options.put("draggable", true);
		options.put("modal", true);
		options.put("width", 600);
		options.put("height", 200);

		Map<String, List<String>> params = new HashMap<>();
		List<String> param = new ArrayList<>();
		param.add("1"); // 设置去除前几行
		params.put("ingnoreRows", param);
		openDialog("/ui/common/upload_excel_dialog", params, options);
	}

	public void onSelectorChosen(SelectEvent event) {
		List<List<String>> objs = (List<List<String>>) event.getObject();
		for (List<String> list : objs) {
			// name; province; defectLevel; defectDevice; defectPosition; defectCause;
			// defectType; occurDate; specifiedSolveDate; solveDate; solved;
			// 名称0 省区1 缺陷等级2 缺陷装置3 缺陷部位4 缺陷原因5 缺陷类型6 发生日期7 规定消缺日期8 消缺日期 9 是否消缺10
			CountProtectDefect cpd = new CountProtectDefect();
			cpd.setName((String) list.get(0));
			cpd.setProvince((String) list.get(1));
			cpd.setDefectLevel((String) list.get(2));
			cpd.setDefectDevice((String) list.get(3));
			cpd.setDefectPosition((String) list.get(4));
			cpd.setDefectCause((String) list.get(5));
			cpd.setDefectType((String) list.get(6));
			cpd.setOccurDate(DateUtil.getDateByString((String) list.get(7), DateUtil.pattern_ymdhms));
			cpd.setSpecifiedSolveDate(DateUtil.getDateByString((String) list.get(8), DateUtil.pattern_ymdhms));
			cpd.setSolveDate(DateUtil.getDateByString((String) list.get(9), DateUtil.pattern_ymdhms));
			cpd.setSolved(list.get(10).equals("是") ? true : false);
			manager.create(cpd);
			countProtectDefects.add(cpd);
		}
		years.clear();
		init();
		addSuccessMessage("文件上传成功！");
		countProtectDefects = manager.findAllCountProtectDefect();
	}

	public void onRowSelect(SelectEvent event) {
		selectCountProtectDefect = (CountProtectDefect) event.getObject();
	}

	public List<String> completeProvince(String query) {
		return provinces;
	}

	public void selectYear() {
		FacesContext con = FacesContext.getCurrentInstance();
		Map<String, String> map = con.getExternalContext().getRequestParameterMap();
		String y = map.get("year").toString();
		year = Integer.parseInt(y);
	}

	public List<CountProtectDefect> getCountProtectDefects() {
		return countProtectDefects;
	}

	public void setCountProtectDefects(List<CountProtectDefect> countProtectDefects) {
		this.countProtectDefects = countProtectDefects;
	}

	public CountProtectDefect getSelectCountProtectDefect() {
		return selectCountProtectDefect;
	}

	public void setSelectCountProtectDefect(CountProtectDefect selectCountProtectDefect) {
		this.selectCountProtectDefect = selectCountProtectDefect;
	}

	public CountProtectDefect getCountProtectDefect() {
		return countProtectDefect;
	}

	public void setCountProtectDefect(CountProtectDefect countProtectDefect) {
		this.countProtectDefect = countProtectDefect;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public List<String> getProvinces() {
		return provinces;
	}

	public void setProvinces(List<String> provinces) {
		this.provinces = provinces;
	}

	public String getDefectLevel() {
		return defectLevel;
	}

	public void setDefectLevel(String defectLevel) {
		this.defectLevel = defectLevel;
	}

	public List<String> getDefectLevels() {
		return defectLevels;
	}

	public void setDefectLevels(List<String> defectLevels) {
		this.defectLevels = defectLevels;
	}

	public boolean getSolved() {
		return solved;
	}

	public void setSolved(boolean solved) {
		this.solved = solved;
	}

	public List<Integer> getYears() {
		return years;
	}

	public void setYears(List<Integer> years) {
		this.years = years;
	}

	public StreamedContent getTemplateFile() {
		return templateFile;
	}

	public void setTemplateFile(StreamedContent templateFile) {
		this.templateFile = templateFile;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

}
